package jinyilw.swing.rlaf.ui;

import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.JComponent;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicHTML;
import java.awt.Color;
import java.awt.Composite;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * @author Jinyi
 */
public class RoundButtonUI extends RButtonUI
{
	@Override
	public void update(Graphics g, JComponent c)
	{
		int h = b.getHeight(), w = b.getWidth();
		boolean isEnabled = b.isEnabled();
		boolean isRollover = b.getModel().isRollover();
		boolean isPressed = b.getModel().isPressed();
		float alpha = !isEnabled ? 0.1f : isRollover ? hoverAlpha : defaultAlpha;
		Graphics2D g2d = (Graphics2D) g;
		Composite composite = g2d.getComposite();
		renderingConfig.setAllAAPainting(g2d);

		GradientPaint p;
		if (isEnabled && isPressed)
		{
			g2d.translate(0.9, 0.9);
			p = new GradientPaint(1, 1, lightBorderColor, 1, h - 1,
					lightBorderColor);
			// alpha = hoverAlpha - 0.08f;
		} else
			p = new GradientPaint(1, 1, lightBorderColor, 1, h - 1,
					darkBorderColor);

		g2d.setComposite(alphaComposite.derive(alpha));
		Color bColor = c.getBackground();
		if (bColor instanceof UIResource)
			g2d.setColor(lightColor);
		else
			g2d.setColor(bColor);
		g2d.fillOval(1, 1, w - 2, h - 2);

		g2d.setComposite(alphaComposite.derive(isEnabled ? 0.8f : 0.3f));

//		if (isEnabled && isRollover)
//			g2d.setColor(RThemeColors.themeColor);
//		else
		g2d.setPaint(p);
		g2d.drawOval(1, 1, w - 2, h - 2);

		g2d.setComposite(alphaComposite.derive(isEnabled ? 1f : 0.25f));
		if (c.getClientProperty(BasicHTML.propertyKey) != null)
			RThemeColor.customForegroundSetting(c);
		paint(g2d, c);

		if (isEnabled && isPressed)
			g2d.translate(-0.9, -0.9);
		renderingConfig.resetAllAAPainting(g2d);
		g2d.setComposite(composite);
	}
}